package com.easysoft.framework.action;

import com.easysoft.commons.utils.StringUtils;
import com.easysoft.commons.utils.WebConstants;
import com.easysoft.framework.annotation.logs.SystemControllerLog;
import com.easysoft.framework.article.model.SysArticleCategory;
import com.easysoft.framework.article.model.SysArticleCategoryQuery;
import com.easysoft.framework.article.model.SysArticleCategoryView;
import com.easysoft.framework.article.service.ArticleCategoryService;
import com.easysoft.framework.beans.Page;
import com.easysoft.framework.workflow.model.SysWorkflow;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 文章分类
 * <p>
 * Created by try on 2017/7/6.
 */
@RestController
@RequestMapping(value = SystemArticleCategoryAction.BASE_VIEW)
public class SystemArticleCategoryAction {

    public static final String BASE_VIEW = "/articleCategory";
    private final static Logger logger = LoggerFactory.getLogger(SystemArticleCategoryAction.class);

    @Autowired
    @Qualifier("service.ArticleCategoryService")
    private ArticleCategoryService articleCategoryService;

    /**
     * 分类列表
     *
     * @param page  分页条件
     * @param query 查询条件
     * @return
     */
    @RequestMapping(value = "/listArticleCategory", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView listArticleCategory(Page page, SysArticleCategoryQuery query) {

        logger.info("Call SysArticleCategoryAction.listArticleCategory({},{})", page.toString(), query.toString());

        List<SysArticleCategoryView> listArticleCategory = articleCategoryService.selectSysArticleCategoryListByPage(page, query);
        List<SysArticleCategoryView> allArticleCategory = articleCategoryService.selectAllCategory();
        ModelAndView mv = new ModelAndView(BASE_VIEW + "/listArticleCategory");
        mv.addObject("page", page); // 返回分页
        mv.addObject("query", query);// 查询条件
        mv.addObject("allArticleCategory", allArticleCategory);
        mv.addObject("listArticleCategory", listArticleCategory); // 返回查询结果集
        return mv;
    }


    @RequestMapping(value = "/editArticleCategory")
    public ModelAndView editArticleCategory(String action,String id) {
        logger.info("Call SysArticleCategoryAction.editArticleCategory({},{})", id,action);
        ModelAndView mv = new ModelAndView(BASE_VIEW + "/editArticleCategory");
        List<SysArticleCategoryView> listSysArticleCategory=articleCategoryService.selectAllCategory();
        if(StringUtils.isNotNullAndBlank(id) && StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("update")){
            SysArticleCategory sysArticleCategory = articleCategoryService.selectSysArticleCategoryByPrimaryKey(id);
            if (sysArticleCategory == null) {
                return mv;
            }
            mv.addObject("sysArticleCategory",sysArticleCategory);
        }
        mv.addObject("listSysArticleCategory",listSysArticleCategory);
        return mv;
    }
    /**
     * 添加分类
     *
     * @param sysArticleCategory
     * @return
     */
    @RequestMapping(value = "/addSysArticleCategory", method = RequestMethod.POST)
    @SystemControllerLog(description = "添加分类")
    public int addSysArticleCategory(String action,SysArticleCategory sysArticleCategory, HttpSession session) {
        logger.info("Call SysArticleCategoryAction.addSysArticleCategory({})", sysArticleCategory.toString());
        if (StringUtils.isNullOrBlank(sysArticleCategory.getCategoryName())) {
            // 分类名称为空
            return 0;
        }
        sysArticleCategory.setOperator((String) session.getAttribute(WebConstants.SESSION_USER_ID));
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("add")) {
            SysArticleCategory parent = articleCategoryService.selectSysArticleCategoryByPrimaryKey(sysArticleCategory.getParentNodes());
            if(parent == null ){
                sysArticleCategory.setNodePath("0");
            }else{
                sysArticleCategory.setNodePath(parent.getNodePath() + "," + parent.getId());
            }
            return articleCategoryService.addSysArticleCategory(sysArticleCategory);
        }
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("update")) {

            SysArticleCategory parent = articleCategoryService.selectSysArticleCategoryByPrimaryKey(sysArticleCategory.getParentNodes());
            if (parent == null){
                sysArticleCategory.setNodePath("0");
            } else {
                sysArticleCategory.setNodePath(parent.getNodePath() + "," + parent.getId());
            }
            return articleCategoryService.updateArticleCategory(sysArticleCategory);
        }
        return 0;

    }

    /**
     * 查询分类信息
     *
     * @param id
     * @return
     */

    @RequestMapping(value = "/viewArticleCategory", method = RequestMethod.POST)
    public SysArticleCategory viewArticleCategory(String id) {
        logger.info("Call SysArticleCategoryAction.viewArticleCategory({})", id);
        return articleCategoryService.selectSysArticleCategoryByPrimaryKey(id);
    }

    /**
     * 更新分类信息
     *
     * @return
     */
    @RequestMapping(value = "/updateArticleCategory", method = RequestMethod.POST)
    @SystemControllerLog(description = "更新分类信息")
    public int updateArticleCategory(SysArticleCategory sysArticleCategory) {
        logger.info("Call SysArticleCategoryAction.updataArticleCategory({})", sysArticleCategory.toString());

        SysArticleCategory temp = articleCategoryService.selectSysArticleCategoryByPrimaryKey(sysArticleCategory.getId());

        if (temp == null) {
            // 修改的记录不存在
            return 0;
        }

        SysArticleCategory parent = articleCategoryService.selectSysArticleCategoryByPrimaryKey(sysArticleCategory.getParentNodes());
        if (parent == null){
            sysArticleCategory.setNodePath("0");
        } else {
            sysArticleCategory.setNodePath(parent.getNodePath() + "," + parent.getId());
        }
        return articleCategoryService.updateArticleCategory(sysArticleCategory);
    }

    /**
     * 查询所有分类
     *
     * @return
     */
    @RequestMapping(value = "/selectCategory", method = RequestMethod.POST)
    public List<SysArticleCategoryView> selectCategory() {
        logger.info("Call SysArticleCategoryAction.selectCategory()");

        return articleCategoryService.selectAllCategory();
    }

    /**
     * 删除文章分类
     *
     * @param id 主键id
     */
    @RequestMapping(value = "/deleteCategory", method = RequestMethod.POST)
    @SystemControllerLog(description = "删除文章分类")
    public int deleteCategory(String id) {
        return articleCategoryService.deleteArticleCategory(id);
    }

    /**
     * 更新文章信息
     *
     * @param articleCategory 文章信息
     */
    @RequestMapping(value = "/updaterCategoryStatus", method = RequestMethod.POST)
    @SystemControllerLog(description = "更新文章信息")
    public int updaterCategoryStatus(SysArticleCategory articleCategory, HttpSession session) {
        logger.info("Call SystemArticleAction.updateSysArticle()");
        articleCategory.setOperator((String) session.getAttribute(WebConstants.SESSION_USER_ID));
        return articleCategoryService.updateArticleCategory(articleCategory);

    }
}
